<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #return_extend="{row}">

    </template>
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
  </list_url>
</template>
<script>
import {iframe, in_right_admin} from "@/js/tool.js";
import {_config} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if (is_del){
      if(in_right_admin('MessageReply/restore')) btns['恢复'] = function(){ app.$refs['list_url'].restore() }
      if(in_right_admin('MessageReply/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }else {
      if(in_right_admin('MessageReply/recycle')) btns['提示'] = function(){
        let url = 'https://'+window.location.hostname+'/home/api/wechat_msg';
        app.$alert(`<h3>微信公众号后台配置接口地址：</h3><p>${url}</p>`, {dangerouslyUseHTMLString:true}).catch(()=>{});
      }
      if(in_right_admin('MessageReply/add')) btns['添加'] = function(){ iframe('message_reply/edit') }
      if(in_right_admin('MessageReply/del')) btns['删除'] = function(){ app.$refs['list_url'].del() }
      if(in_right_admin('MessageReply/recycle')) btns['回收站'] = function(){ iframe('message_reply/index?is_del=1') }
    }
    let type_map = _config('status_alias_map.message_reply.type');
    let rule_type_map = {
      'eq':'全等','like':'包含','regexp':'正则规则',
      'subscribe':'订阅','click':'点击','scancode_push':'扫码推事件','scancode_waitmsg':'扫码待回复','pic_sysphoto':'拍照发图','pic_photo_or_album':'拍照或相册发图','pic_weixin':'微信相册发图','location_select':'发送位置'};
    let forms_config = {
      type: {label:'消息类型',name:'where[type][eq]',type:'select_one',config:type_map,value:''},
      keyword: {label:'关键词',name:'where[rule_value|return_data|return_extend][like]',type:'text_one',value:''},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: 'hidden', order:true},
      type:{name:'type', label:'消息类型', width:'120px', type:'select_one', config:type_map},
      rule_type:{name:'rule_type', label:'规则类型', type:'select_one', config:rule_type_map, width:'120px'},
      rule_value:{name:'rule_value', label:'规则内容', type:'text_one', width: '100px'},
      return_type:{name:'return_type', label:'回复类型', width:'70px', type:'select_one', config:{text:'文本',image:'图片素材',voice:'语音素材',video:'视频素材',music:'音乐',news:'图文'}},
      return_data:{name:'return_data', label:'回复数据', min_width: '60px'},
      return_extend:{name:'return_extend', label:'扩展数据', min_width:'60px'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    if (!_config('this_tenant.id')){
      list_cols['tenant_id'] = {name:'tenant_id', label:'租户', width:'65px', fixed:'right'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'80px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/message_reply/'+(is_del?'recycle':'list'),
        title: "消息回复规则列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('MessageReply/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('message_reply/edit?'+s94Mjs.param_encode(param))
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

  }
}
</script>
